<?php
namespace app\index\controller;

use app\Logined;
use think\facade\Config;
use think\facade\Db;

class Douyin extends Logined
{
    public function create_pay(){
        $client = new \GuzzleHttp\Client(['verify' => false ]);
        $out_order_no = uniqid();

        $params = [
            'out_order_no'  =>  $out_order_no,
//            'total_amount'  =>  (int)bcmul($this->param['amount'], 100, 0),
            'total_amount'  =>  1,
            'subject'       =>  $this->param['subject'],
            'body'          =>  $this->param['body'],
            'valid_time'    =>  172000,
            'notify_url'    =>  $this->request->domain()."/index/index/douyin_pay_notify",
        ];

        Db::name('charge_order')->insert([
            'order_sn'      =>  $out_order_no,
            'user_id'       =>  $this->user_id,
            'charge_option' =>  json_encode($this->param),
            'pay_money'     =>  $this->param['amount'],
            'is_pay'        =>  0,
            'add_time'      =>  time()
        ]);

//        dump($params);die;
        $sign = douyinPaySign($params);

        $params['app_id']=Config::get('app.douyin_xcx_app_id');
        $params['sign']=$sign;

        $res = $client->request('POST', "https://developer.toutiao.com/api/apps/ecpay/v1/create_order",[
            'headers'=>[
                'Content-Type'=>'application/json',
            ],
            'body'=>json_encode($params)
        ]);
        $response=$res->getBody()->getContents();
        if(empty($response)){
            return $this->ajax_return(0,'获取token失败',[
                '$response'=>$response,
            ]);
        }
        $response=json_decode($response,true);
        return $this->success('请求成功',[
            'order_data'=>$response,
            '$params'=>$params,
        ]);

    }

    public function withdraw(){
        $client = new \GuzzleHttp\Client(['verify' => false ]);
        $out_order_id = uniqid();
        if(!$this->param['amount']){
            return $this->error('请输入金额');
        }
        $userInfo = $this->user_info;
        if($this->param['amount'] > $userInfo['money']){
            return $this->error('余额不足');
        }
        $afterMoney = bcsub($userInfo['money'], $this->param['amount']);

        $params = [
            'thirdparty_id'     =>  1,
            'merchant_uid'      =>  1,
            'channel_type'      =>  $this->param['channel_type'],
//            'withdraw_amount'   =>  (int)bcmul($this->param['amount'], 100, 0),
            'withdraw_amount'   =>  1,
            'out_order_id'      =>  $out_order_id
        ];
        $sign = douyinPaySign($params);
        $params['app_id'] = Config::get('app.douyin_xcx_app_id');
        $params['sign'] = $sign;

        $res = $client->request('POST', "https://developer.toutiao.com/api/apps/ecpay/saas/merchant_withdraw",[
            'headers'=>[
                'Content-Type'=>'application/json'
            ],
            'body'  =>  json_encode($params)
        ]);
        $response=$res->getBody()->getContents();
        if(empty($response)){
            return $this->ajax_return(0,'获取token失败',[
                'response'     =>  $response,
            ]);
        }
        $response=json_decode($response,true);

        if($response['err_no'] != 0){
            return $this->error('提现失败',[
                'response'          =>  $response,
                'params'            =>  $params
            ]);
        }

        Db::name('user')->where('user_id', $this->user_id)->update(['money', $afterMoney]);
        Db::name('moneylog')->insert([
            'add_time'      =>  time(),
            'user_id'       =>  $this->user_id,
            'log_type'      =>  1,
            'up_or_down'    =>  2,
            'change_num'    =>  $this->param['amount'],
            'before_num'    =>  $userInfo['money'],
            'after_num'     =>  $afterMoney,
            'title'         =>  '提现'
        ]);

        return $this->success('提现成功', [
            'response'      =>  $response,
            'params'        =>  $params
        ]);
    }


    public function alipay_withdraw(){
        if(!$this->param['amount']){
            return $this->error('请输入金额');
        }
        $userInfo = $this->user_info;
        if($this->param['amount'] > $userInfo['money']){
            return $this->error('余额不足');
        }
        $afterMoney = $userInfo['money']*1+ $this->param['amount']*1;

        Db::name('user')->where('user_id', $this->user_id)->update(['money'=> $afterMoney]);

        Db::name('tixian')->insert([
            'add_time'      =>  time(),
            'user_id'       =>  $this->user_id,
            'money'      =>  $this->param['amount'],
            'account'      =>  $this->param['account'],
            'name'      =>  $this->param['name'],
            'title'         =>  '提现',
            'type'         =>  '支付宝',
        ]);

        Db::name('moneylog')->insert([
            'add_time'      =>  time(),
            'user_id'       =>  $this->user_id,
            'log_type'      =>  1,
            'up_or_down'    =>  2,
            'change_num'    =>  $this->param['amount'],
            'before_num'    =>  $userInfo['money'],
            'after_num'     =>  $afterMoney,
            'title'         =>  '提现'
        ]);

        return $this->success('提现成功', [
        ]);
    }

}
